package com.ruoyi.wxarea.controller;



import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.wxarea.mapper.AreaMapper;
import com.ruoyi.wxarea.pojo.AddressCate;
import com.ruoyi.wxarea.pojo.Area;
import com.ruoyi.wxarea.pojo.vo.AddressCateVO;
import com.ruoyi.wxarea.service.IAddressCateService;
import com.ruoyi.wxarea.service.IAreaService;
import com.ruoyi.wxcommon.common.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凡哥哥
 * @since 2024-05-10
 */
@RestController
@RequestMapping("/area/address-cate")
public class AddressCateController extends BaseController {
//    @Autowired
//    IWxUserLoginService wxUserLoginService;
    @Autowired
    IAreaService areaService;
    @Autowired
    IAddressCateService iAddressCateService;
    /*@PostMapping("/get/areaInfo")
    public Result getAreaInfo(Integer aId,String name,Integer userId){
        //iAddressCateService.

        return Result.SUCCESS(areaService.getById(aId));
    }*/
    @PostMapping("/get/areaInfo")
    public Result getAreaInfo(Integer pageIndex, Integer pageSize, Integer aId){
        return iAddressCateService.addressCateByAreaId(pageIndex, pageSize, aId);
    }
    /**
     * 查询学校地区管理列表
     */
    @RequiresPermissions("campusassistant:addresscate:list")
    @GetMapping("/list")
    public TableDataInfo list(AddressCate addressCate)
    {
        startPage();
        List<AddressCate> list = iAddressCateService.selectAddressCateList(addressCate);
        List<AddressCateVO> cateVOList = list.stream().map(item -> {
            AddressCateVO addressCateVO = new AddressCateVO();
            BeanUtils.copyProperties(item, addressCateVO);
            Long aId = item.getAId();
            Area area = areaService.getById(aId);
            addressCateVO.setAName(area.getName());
            return addressCateVO;
        }).collect(Collectors.toList());
        return getDataTable(cateVOList);
    }
    /**
     * 修改学校地区管理
     */
    @RequiresPermissions("campusassistant:addresscate:edit")
    @Log(title = "学校地区管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AddressCate addressCate)
    {
        return toAjax(iAddressCateService.updateAddressCate(addressCate));
    }
    @Resource
    private AreaMapper areaMapper;
    @GetMapping("/areaList")
    public List list()
    {
        return areaMapper.areaList();
    }

    /**
     * 导出学校地区管理列表
     */
    @RequiresPermissions("campusassistant:addresscate:export")
    @Log(title = "学校地区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AddressCate addressCate)
    {
        List<AddressCate> list = iAddressCateService.selectAddressCateList(addressCate);
        ExcelUtil<AddressCate> util = new ExcelUtil<AddressCate>(AddressCate.class);
        util.exportExcel(response, list, "学校地区管理数据");
    }

    /**
     * 获取学校地区管理详细信息
     */
    @RequiresPermissions("campusassistant:addresscate:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(iAddressCateService.selectAddressCateById(id));
    }

    /**
     * 新增学校地区管理
     */
    @RequiresPermissions("campusassistant:addresscate:add")
    @Log(title = "学校地区管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AddressCate addressCate)
    {
        return toAjax(iAddressCateService.insertAddressCate(addressCate));
    }



    /**
     * 删除学校地区管理
     */
    @RequiresPermissions("campusassistant:addresscate:remove")
    @Log(title = "学校地区管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(iAddressCateService.deleteAddressCateByIds(ids));
    }

}

